package com.widget;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HomePage  extends Activity
{
	String username,ipaddress;
	EditText etName,etIpaddress;
	Button btOkay;
	RelativeLayout rl;
	TextView tvName,tvPassword,tvRemember;
	CheckBox cb;
	SharedPreferences preferences;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        cb=(CheckBox)findViewById(R.id.chkRememberMe);
        etName=(EditText)findViewById(R.id.etUname);
        etIpaddress=(EditText)findViewById(R.id.etIpaddress);
        btOkay=(Button)findViewById(R.id.btnOkay);
       
//        etIpaddress=new EditText(this);
        
        
        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter() {
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (end > start) {
                    String destTxt = dest.toString();
                    String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
                    if (!resultingTxt.matches ("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) { 
                        return "";
                    } else {
                        String[] splits = resultingTxt.split("\\.");
                        for (int i=0; i<splits.length; i++) {
                            if (Integer.valueOf(splits[i]) > 255) {
                                return "";
                            }
                        }
                    }
                }
            return null;
            }

			

			
        };
        etIpaddress.setFilters(filters);

        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        username = preferences.getString("username", "none");
        ipaddress = preferences.getString("ipaddress", "none");
       
        	etName.setText(username);
        	etIpaddress.setText(ipaddress);
        
         btOkay.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		
					try {
						Class testClass = Class.forName("com.widget.MainActivity");
						
						Intent testIntent1 = new Intent(HomePage.this,testClass);
						testIntent1.putExtra("USERNAME", etName.getText().toString());
						testIntent1.putExtra("IPADDR",etIpaddress.getText().toString());
						startActivity(testIntent1);
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		});
         
        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				SharedPreferences.Editor editor = preferences.edit();
				if(buttonView.isChecked()){
					if(validateLoginDetails())
					{
						//editor.remove("username");
						//editor.remove("ipaddress");
						editor.clear();
						//Toast.makeText(this, "Entered ButtonClick Checked", Toast.LENGTH_LONG);
						
						editor.putString("username", etName.getText().toString());
						editor.putString("ipaddress", etIpaddress.getText().toString());
						editor.commit();
					}
					else{
						//Display Error Message
					}
					
				}
				else{
					
				}
				
			}
		});
       
    }
    
    boolean validateLoginDetails(){
//    	if(username.equals("none") || ipaddress.equals("none"))
//    		return false;
    	
    	
    	return true;
    	
    }


}
